//
//  BDLevelViewController.m
//  手机号码验证
//
//  Created by Chuz NetTech on 17/9/11.
//  Copyright © 2017年 Chuz NetTech. All rights reserved.
//

#import "BDLevelViewController.h"
#import "BDGeneralCell.h"
#import "BDUserModel.h"
#import "BDLevelModel.h"
@interface BDLevelViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UIButton *selectedButton;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *datasourceArray;
@end

@implementation BDLevelViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = [self.generalModel.title stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    
    self.datasourceArray = [NSMutableArray array];
    
    [self requestData];
    
    [self createUI];
}

-(void)createUI{
    self.tableView = [[UITableView alloc] init];
    self.tableView.left = 0;
    self.tableView.top = kBDTopHeight;
    self.tableView.size = CGSizeMake(kBDScreenWidth, kBDScreenHeight - kBDTopHeight);
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    [self.view addSubview:self.tableView];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datasourceArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BDGeneralModel *model = self.datasourceArray[indexPath.row];
    BDGeneralCell *cell = [BDGeneralCell cellForTableView:self.tableView model:model indexPath:indexPath identifier:@"CELL"];
    cell.backgroundColor = [UIColor whiteColor];
    cell.subviewBlock = ^(BDGeneralCell *cell){
        cell.imageView.top = 10;
        cell.imageView.left = 10;
        cell.imageView.size = CGSizeMake(50, 50);
        cell.imageView.layer.cornerRadius = 25;
        cell.imageView.layer.masksToBounds = YES;
        
        
        cell.textLabel.left = cell.imageView.right + 10;
        cell.textLabel.font = [UIFont systemFontOfSize:17];
        cell.textLabel.textColor = [UIColor colorWithHexString:@"1c1c1c"];
        [cell.textLabel sizeToFit];
        cell.textLabel.centerY = cell.height / 2;
        
        cell.aImageView.left = cell.textLabel.right + 6;
        [cell.aImageView sizeToFit];
        cell.aImageView.centerY = cell.textLabel.centerY;
        
    };
    
    return cell;
}

-(CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}

//-(CGFloat )tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    return 35;
//}
//
//-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//    UIView *headerView = [[UIView alloc] init];
//    headerView.backgroundColor = [UIColor clearColor];
//    headerView.left = 0;
//    headerView.top = 0;
//    headerView.size = CGSizeMake(kBDScreenWidth, 35);
//    
//    UILabel *numberLabel = [[UILabel alloc] init];
//    numberLabel.left = 10;
//    numberLabel.top = 5;
//    numberLabel.text = ASLocalizedString(@"3个");
//    numberLabel.textColor = [UIColor colorWithHexString:@"ff8400"];
//    numberLabel.font = [UIFont systemFontOfSize:15];
//    [numberLabel sizeToFit];
//    [headerView addSubview:numberLabel];
//    
//    return headerView;
//}

-(void)requestData{
    
    NSString *url = [NSString stringWithFormat:@"%@MLMChildren?level=%@&page=%@&size=%@",KBDBaseURL,self.levelString,@"1",@"10"];
    
    NSString *headValid = [BDUserModel getHeaderValidation];
    RACSignal *signal = [[BDNetworkingServer yy_GET:url parameters:nil validation:headValid automaticProcessingError:YES] execute:nil];
    
    [signal subscribeNext:^(id  _Nullable x) {
        
        NSMutableArray *array = [NSMutableArray arrayWithArray:[BDLevelModel mj_objectArrayWithKeyValuesArray:x[@"list"]]];
        for (int i = 0; i < array.count; i++) {
            BDLevelModel *model = array[i];
            //            if (model.agent_level == 0) {
            //                levelString = @"";
            //            }else{
            //                levelString = @"user_vip";
            //            }
            [_datasourceArray addObject:[BDGeneralModel new].titleEqual(model.children[@"nickname"]).imageEqual(model.children[@"cover"]).placeImageEqual(@"avatar").imageTypeEqual(BDGeneralCellImageTypeURL)];

        }
        self.isEnd = [x[@"is_end"] boolValue];
        [self setTableiewFooterViewStateFor:self.tableView datasourceCount:self.datasourceArray.count];
        [self.tableView reloadData];
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
